package com.gopay.checkorder.service.alarm;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 订单报警数据采集者
 * 
 * @OrderAlarmDataCollector.java
 * @author fanghw
 * @2016年1月8日 下午4:33:58 www.gopay.com.cn Inc.All rights reserved.
 */
public class OrderAlarmDataCollector {

    private Map<String, Integer> totalMap = new ConcurrentHashMap<String, Integer>();
    private Map<String, Integer> failMap = new ConcurrentHashMap<String, Integer>();

    public synchronized void addTotal(String bankCode) {
        Integer num = totalMap.get(bankCode);
        num = num == null ? 1 : num + 1;
        totalMap.put(bankCode, num);
    }

    public synchronized void addFail(String bankCode) {
        Integer num = failMap.get(bankCode);
        num = num == null ? 1 : num + 1;
        failMap.put(bankCode, num);
    }
    
    public Integer getTotal(String bankCode) {
        Integer num = totalMap.get(bankCode);
        return num == null ? 0 : num;
    }

    public  Integer getFail(String bankCode) {
        Integer num = failMap.get(bankCode);
        return num == null ? 0 : num;
    }

    public double calFailScale(String bankCode) {
        Integer total = getTotal(bankCode);
        Integer fail = getFail(bankCode);

        double scale = 0;
        if (total > 0) {
            scale = (((double) fail) / ((double) total)) * 100;
        }

        return scale;
    }
}
